<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller\Home;

class HomeOrderController extends Controller
{
    //订单列表
    public function getIndex(Request $request)
    {   
      if(!session('name')){
            return redirect('/home/login/login');
        }
       	//接收数据
        // $goods = $request->all();
        $goods = $request->except('_token','key');
        $user_name = session('name');
       	$arr = [];
        $zongjia = 0;
        foreach($goods as $k=>$v){
          foreach($v as $kk=>$vv){
            $arr[$kk][$k]=$vv;
          }
        }
        $data = [];
        foreach($arr as $key=>$value){
          $orders = DB::table('goods')->where('id',$value['id'])->first();
          $orders->uname = $user_name;
          $orders->num = $value['amt'];
          $orders->dingdan_size = $value['dingdan_size'];
          $orders->dingdan_color = $value['dingdan_color'];
          $orders->dingdan_time = time();
          $data[] = $orders;
          $zongjia += $orders->price*$orders->num;
        }
        // dd($orders);
        // 用户邮寄地址
        $dingdan_tijiao = DB::table('address')->where('user_name',$user_name)->get();

       	return view('home.xiangqing.dingdan_tijiao',['orders'=>$data,'zongjia'=>$zongjia,'dingdan_tijiao'=>$dingdan_tijiao]);
    	
    }
    //订单提交 
    public function postTijiao(Request $request){
      $data = $request->only(['dingdan_id','price','status','atime','dingdan_num','dingdan_pic','dingdan_color','dingdan_size','dingdan_content','status','dingdan_name']);
      // $data = $request->all();
      // dd($data);
      $zhifu_fangshi = $request->input('payway');
      $arr = [];
      foreach($data as $k=>$v){      
        foreach($v as $kk=>$vv){
          $arr[$kk][$k] = $vv;
        }

      }
      $lice_name = '淘气一哥店主';
      $uname = session('name');
      //通过name查找
        $address = DB::table('address')->where('user_name',$uname)->where('statuss','2')->first();
        $dizhi_id = $address->id;
      $zongjia = 0;
      foreach($arr as $key=>$value){
        $res = DB::table('order')->insert(['dizhi_id'=>$dizhi_id,'zhifu_fangshi'=>$zhifu_fangshi,'uname'=>$uname,'utname'=>$value['dingdan_name'],'dingdan_id'=>$value['dingdan_id'],'dingdan_num'=>$value['dingdan_num'],'price'=>$value['price'],'status'=>$value['status'],'atime'=>$value['atime'],'dingdan_pic'=>$value['dingdan_pic'],'dingdan_name'=>$value['dingdan_name'],'dingdan_color'=>$value['dingdan_color'],'dingdan_size'=>$value['dingdan_size'],'dingdan_content'=>$value['dingdan_content']]);
        $zongjia += $value['price']*$value['dingdan_num'];
      }
      //事物处理
      DB::beginTransaction();//开启事务
      $user = DB::table('user')->where('username',$uname)->first();
      $uid = $user->id;
      $lice_users = DB::table('dayingjia')->where('lice_name',$lice_name)->first();
      $lid = $lice_users->id;
      $user_account = $user->account;

      // dd($address);
      if($user_account < $zongjia){
        DB::rollBack();
        return back()->with('success','您的账户余额不足,请换其他方式支付');

      }
      $ures = DB::update('update user set account = account - '.$zongjia.' where id = '.$uid);
      if(!$ures == 1){
        DB::rollBack();
        // DB::table('order')->where('id',$uid)->update(['status'=>2]);
        return redirect('welcome')->with('success','支付失败');

      }
      // dd($ures);
      $lres = DB::update('update dayingjia set lice_account = lice_account+'.$zongjia.' where id = '.$lid);
        
      // if(!$ures == 1){
      //   DB::rollBack();
      //   DB::table('order')->where('id',$uid)->update(['status'=>2]);
      //   return redirect('welcome')->with('success','支付失败');
      // }
      DB::commit();//事物提交
      $request->session()->forget('cart');
      //提交成功 页面跳转
      return redirect('/home/order/xiangqing')->with('success','恭喜您,订单提交成功.');
    }
    public function getXiangqing(){
      // dd($request->all());
        if(!session('name')){
            return view('home.login.login');
        }
        $uname = session('name');
        // dd($uname);
      $address = DB::table('address')->where('user_name',$uname)->where('statuss','2')->first();

      if($address){
        $dingdanss = 1;
        $dizhi_id = $address->id; 
        $orders = DB::table('order')
              ->join('address', 'order.dizhi_id', '=', 'address.id')
              ->select('order.*', 'address.sheng','address.shi','address.xian','address.content','address.dizhi_name','address.phone')
              ->where('order.uname',$uname)
              ->paginate(3);

              // dd($orders);`
        return view('home.xiangqing.xiangqing',['orders'=>$orders,'dingdanss'=>$dingdanss]);       
      }else{
        $dingdanss = 0;
        return view('home.xiangqing.xiangqing',['dingdanss'=>$dingdanss]); 
      }
   }
   //修改状态
   public function getZhuangtai(Request $request)
   {
      $id = $request->input('did');
      // dd($id);
      $res = DB::table('order')->where('id',$id)->update(['status'=>3]);
      if($res){
          echo 1;
      }else{
          echo 0;
      }
   }
    
}
